﻿using MediatR;
using UserLicense.Applicationes.Command;
using UserModels;
using UserRepositorys.Intface;

namespace UserLicense.Applicationes.CommandHandler
{
    public class UpadministratorCommandHandler : IRequestHandler<UpadministratorCommand, int>
    {
        public IUserRepository _IUserRepository;
        public UpadministratorCommandHandler(IUserRepository userRepository)
        {
            _IUserRepository = userRepository;
        }
        public async Task<int> Handle(UpadministratorCommand request, CancellationToken cancellationToken)
        {
            var dellit=await _IUserRepository.GetAllAsync(x=>x.StoreId==request.StoreId);
            var dellits = dellit.FirstOrDefault();
            if (dellit.Count()>0) {
                UserInfoModel models = new UserInfoModel();
                models.UserId = dellits.UserId;
                models.UserName = dellits.UserName;
                models.UserAccount = dellits.UserAccount;
                models.UserPass = dellits.UserPass;
                models.UserPhone = dellits.UserPhone;
                models.UserEmail = dellits.UserEmail;
                models.UserSex = dellits.UserSex;
                models.DeptId = dellits.DeptId;
                models.RoleId = dellits.RoleId;
                models.StoreId = 0;
                models.IsDelete = dellits.IsDelete;
                models.CreateBy = dellits.CreateBy;
                models.UpdateBy = dellits.UpdateBy;
                models.CreateTime = dellits.CreateTime;
                models.UpdateTime = dellits.UpdateTime;
                var rlis = await _IUserRepository.Updateasync(models);
            }
            var res=await _IUserRepository.GetAllAsync(x=>x.UserAccount==request.UserAccount&&x.UserName==request.UserName);
            var list = res.FirstOrDefault();
            if (res.Count()>0)
            {
                UserInfoModel models= new UserInfoModel();
                models.UserId = list.UserId;
                models.UserName = list.UserName;
                models.UserAccount=list.UserAccount;
                models.UserPass=list.UserPass;
                models.UserPhone=list.UserPhone;
                models.UserEmail=list.UserEmail;
                models.UserSex = list.UserSex;
                models.DeptId=list.DeptId;
                models.RoleId=list.RoleId;
                models.StoreId = request.StoreId;
                models.IsDelete = list.IsDelete;
                models.CreateBy=list.CreateBy;
                models.UpdateBy=list.UpdateBy;
                models.CreateTime=list.CreateTime;
                models.UpdateTime=list.UpdateTime;
                var rlis=await _IUserRepository.Updateasync(models);
                return rlis;
            }
            return 0;

          
        }
    }
}
